'use strict'

const Service = require('egg').Service;

class StatusService extends Service {
    
    checkCommitTs(statusArr) {

        const currentDate  = new Date();
        const currentYear  = currentDate.getFullYear();
        const currentMonth = currentDate.getMonth() + 1;
        const currentDay   = currentDate.getDate();

        const commitDate  = new Date(statusArr[0].commitTs);
        const commitYear  = commitDate.getFullYear();
        const commitMonth = commitDate.getMonth() + 1;
        const commitDay   = commitDate.getDate();

        if ((currentYear === commitYear) && (currentMonth === commitMonth) && (currentDay === commitDay)) {
            return false;
        }
        return true;
    }

    async getStatusByGroup(group) {
        const { ctx, app } = this;
        const currentTs = new Date(new Date().toLocaleDateString()).getTime();
        const statusArr = await ctx.model.Status.find({ group, commitTs: { $gte: currentTs } }).sort({'_id': -1});
        const list = [];
        for (let i = 0; i < statusArr.length; i++) {
            const { userid, username, status, commitTs } = statusArr[i];
            const newItem = { userid, username, status, commitTs };
            const messages = await ctx.model.Messages.find({ userid }).sort({'_id': -1});
            newItem.hasNewMessage = false;
            for (let j = 0; j < messages.length; j++) {
                const { replyId } = messages[j];
                if (!replyId) {
                    newItem.hasNewMessage = true;
                    break;
                }
            }
            list.push(newItem);
        }
        return list;
    }

    async getStatus(userid) {
        const { ctx, app } = this;
        const statusArr = await ctx.model.Status.find({ userid }).sort({'_id': -1}).limit(1);
        if (!statusArr || statusArr.length != 1) return;
        
        const status = statusArr[0];
        return status;
    }

    async getStatusList() {
        const { ctx, app } = this;
        const currentTs = new Date(new Date().toLocaleDateString()).getTime();
        // const statusArr = await ctx.model.Status.find({ commitTs: { $gte: currentTs } }).sort({'_id': -1});
        const statusArr = await ctx.model.Status.find().sort({'_id': -1});
        
        let statusMap = {};
        statusArr.forEach(item => {
            const { userid } = item;
            if (!statusMap[userid]) {
                statusMap[userid] = item;
            }
        })

        let statusList = [
            { group: 0, status: 0, value: 0}, { group: 0, status: 1, value: 0}, { group: 0, status: 2, value: 0}, { group: 0, status: 3, value: 0},
            { group: 1, status: 0, value: 0}, { group: 1, status: 1, value: 0}, { group: 1, status: 2, value: 0}, { group: 1, status: 3, value: 0},
            { group: 2, status: 0, value: 0}, { group: 2, status: 1, value: 0}, { group: 2, status: 2, value: 0}, { group: 2, status: 3, value: 0},
            { group: 3, status: 0, value: 0}, { group: 3, status: 1, value: 0}, { group: 3, status: 2, value: 0}, { group: 3, status: 3, value: 0},
            { group: 4, status: 0, value: 0}, { group: 4, status: 1, value: 0}, { group: 4, status: 2, value: 0}, { group: 4, status: 3, value: 0},
            { group: 5, status: 0, value: 0}, { group: 5, status: 1, value: 0}, { group: 5, status: 2, value: 0}, { group: 5, status: 3, value: 0},
            { group: 6, status: 0, value: 0}, { group: 6, status: 1, value: 0}, { group: 6, status: 2, value: 0}, { group: 6, status: 3, value: 0}
        ];

        Object.keys(statusMap).forEach(key => {
            const { status, group } = statusMap[key];
            const index = (group * 4) + status;
            statusList[index].value += 1;
        })

        return statusList;
    }

    async create(userid, username, group, status) {
        const { ctx, app } = this;
        const currentDate  = new Date();
        let statusArr = await ctx.model.Status.find({ userid }).sort({'_id': -1}).limit(1);
        if (statusArr.length === 0 || this.checkCommitTs(statusArr)) {
            await ctx.model.Status.create({
                userid,
                username,
                group,
                status,
                commitTs: currentDate.getTime(),
            }, err => {
                console.log(err);
                return false;
            })
        } else {
            let statusInfo = statusArr[0];
            statusInfo.status = status;
            statusInfo.commitTs = currentDate.getTime();
            await ctx.model.Status.update({ _id: statusInfo._id }, statusInfo, err => {
                console.log(err);
                return false;
            })
        }
        return true;
    }
}

module.exports = StatusService;